package com.ny.demo;

import java.sql.*;
import java.util.Scanner;


//通过java去操作数据库：通过java去执行sql
//1.导入驱动jar包  2.注册驱动
//3.获取数据库连接  4.编写SQL语句  5.执行SQL语句 6.处理结果
public class Login {

    private static final String URL = "jdbc:mysql://localhost:3306/usersdb";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "123456";
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(URL,DB_USERNAME,DB_PASSWORD);
        String sql = "select * from userdb where username=? and password =?";
        PreparedStatement ps = conn.prepareStatement(sql);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();

        ps.setString(1,username);
        ps.setString(2,password);
        ResultSet  rs = ps.executeQuery();
        if(rs.next()){
            System.out.println("登录成功！");
        }else{
            System.out.println("登录失败！");
        }
    }
}
